/**
 * Looker  新闻整合系统
 * Author  :solosky
 * File    :DeleteModifier.java
 * Date    :May 14, 2009
 * Lisence : Apache License 2.0 
 */
package net.looker.monitor.modifier;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;

import net.looker.config.ConfigManager;
import net.looker.data.DataManager;
import net.looker.data.News;
import net.looker.util.MysqlQuery;
import net.looker.util.URLUtils;

/**
 * @author solosky
 *
 */
public class DeleteModifier extends Modifier
{

	/**
	 * @param news
	 */
	public DeleteModifier(News news) 
	{
		super(news);
	}

	/* (non-Javadoc)
	 * @see net.looker.work.Work#startWork()
	 */
	@Override
	public void startWork() 
	{
		try {
			deleteMedia();
		} catch (SQLException e) {
			Logger.getLogger("looker").warning("删除更改器：SQL语句错误->"+e.getMessage());
		}
		DataManager.getInstance().delete(news);
	}
	
	/**
	 * 删除新闻文件
	 * @throws SQLException 
	 */
	private void deleteMedia() throws SQLException
	{
		//查询该新闻的附件
		String sql   = "SELECT * FROM #media WHERE newshash='"+URLUtils.hash(news.getUrl())+"';";
		ResultSet rs = MysqlQuery.getInstance().query(sql);
		File media   = null;
		String path  = null;
		while(rs.next())
		{
			path = ConfigManager.getInstance().getLookerConfig().get("media.saveDir")+'/'
					+rs.getString("savedir")+'/'+rs.getString("date")+'_'+rs.getString("name");
			media = new File(path);
			if(media.exists())
				media.delete();
			else
				Logger.getLogger("looker").warning("删除更改器：文件不存在->"+path);
		}
	}
}
